package com.iseastar.guojiang.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.iseastar.guojiang.MainActivity;
import com.iseastar.guojiang.loc.BaiduLocationService;

import java.util.List;

import droid.frame.App;

/**
 * 界面跳转相关的
 *
 * @author coffee<br>
 * 2016-11-23下午3:35:08
 */
public class Intents {

    /**
     * 裹将扫码绑定
     */
    public final static String guojiang_order_bind = "guojiang_order_bind";
    /**
     * 裹将扫驿站二维码 -- 获取所有包裹列表
     */
    public final static String guojiang_for_station_parcels = "guojiang_for_station_parcels";
    /**
     * 裹将扫驿站的包裹、扫包裹条形码、核对包裹
     */
    public final static String guojiang_check_parcel = "guojiang_check_parcel";
    /**
     * 驿站扫码、收取用户包裹
     */
    public final static String station_for_user_parcel = "station_for_user_parcel";

    public final static String station_scan_for_input = "录入包裹";

    /**
     * 身份认证拍照
     */
    public final static String station_scan_for_identity = "身份认证";
    /**
     * 为店铺拍照
     */
    public final static String station_scan_for_shop = "店铺拍照";
    /**
     * 火箭侠任务扫描二维码
     */
    public final static String courier_scan_for_task = "火箭侠任务扫描二维码";
    /**
     * 火箭侠个人中心，扫描火箭侠二维码
     */
    public final static String courier_scan_for_tansfer = "扫描火箭侠二维码";
    /**
     * 火箭侠扫描绑定袋子
     */
    public final static String courier_scan_bind_bagcode = "火箭侠扫描绑定袋子";

    /**
     * 拨号或者直接呼叫
     *
     * @param phone
     */
    public static void dialOrCall(String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            App.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /**
     * s
     * 启动主类、并清除task中主类之上的Activity
     */
    public static void startLauncherActivity(Activity context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public static void startLauncherActivity1(Context context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 启动主类、并清除task中主类之上的Activity
     */
    public static void startMainActivityClearTop(Activity context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("clearTop", true);
        context.startActivity(intent);
    }

    /**
     * 启动主类、并清除task中主类之上的Activity
     */
    public static void startTakeMainActivity(Activity context) {
        Intent intent = new Intent();
        intent.setClass(context, com.shouhuobao.take.MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("clearTop", true);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param msgId   默认-1
     */
    public static void startBaiduLocation(Context context, Integer msgId) {
        Intent intent = new Intent();
        intent.setClass(context, BaiduLocationService.class);
        if (msgId != null) {
            intent.putExtra("msgId", msgId);
        }
        context.startService(intent);
    }

    /**
     * 导航
     *
     * @param type 0 百度  1 高德
     * @parmam 前两个坐标是 百度的,后俩是高德的
     * {@link "http://lbsyun.baidu.com/index.php?title=uri/api/android","http://lbs.amap.com/api/amap-mobile/guide/android/navigation"}
     */
    public static void navigation(double latBd, double lngBd, double latGd, double lngGd, int type) {
        try {
            Context context = App.getContext();
            //高德地图导航
            if (1 == type) {
                if (isInstallGaodeMap(context)) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    // 将功能Scheme以URI的方式传入data
                    String url = "amapuri://route/plan/?dlat=" + latGd + "&dlon=" + lngGd + "&dev=0&t=3&pkg=" + context.getPackageName();
                    //                    Uri uri = Uri.parse("androidamap://navi?sourceApplication=" + context.getPackageName() + "&poiname=&" + //
                    //                            "lat=" + latGd + "&lon=" + lngGd + "&dev=1&style=2&t=3");
                    Uri uri = Uri.parse(url);
                    intent.setData(uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    // 启动该页面即可
                    context.startActivity(intent);
                } else {
                    String uriStr = "http://uri.amap.com/marker?position=" + lngGd + "," + latGd + "&src=mypage&coordinate=gaode&callnative=0";
                    //                    Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                    Uri uri = Uri.parse(uriStr);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            } else {//默认百度
                if (isInstallBaiduMap(context)) {
                    Intent intent = new Intent();
                    //自定义打点，且在指定坐标点上显示点的名称和内容信息
                    intent.setData(Uri.parse("baidumap://map/direction?destination=" + latBd + "," + lngBd + "&mode=riding"));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                } else {
                    //                    Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                    //                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    //                    context.startActivity(intent);
                    String uriStr = "http://api.map.baidu.com/geocoder?location=" + latBd + "," + lngBd + //
                            "&output=html&src=" + context.getPackageName();
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriStr));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断客户端是否安装了百度地图
     *
     * @param context
     * @return
     */
    public static boolean isInstallBaiduMap(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        if (packageInfos != null) {
            for (PackageInfo packageInfo : packageInfos) {
                if (packageInfo.packageName.equals("com.baidu.BaiduMap")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断客户端是否安装了高德地图
     *
     * @param context
     * @return
     */
    public static boolean isInstallGaodeMap(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        if (packageInfos != null) {
            for (PackageInfo packageInfo : packageInfos) {
                if (packageInfo.packageName.equals("com.autonavi.minimap")) {
                    return true;
                }
            }
        }
        return false;
    }
}
